
import { config } from '../config.js';
const tips = {
    1: '抱歉，出现了一个错误',
    1005: 'appkey无效，请前往www.7yue.pro申请',
    3000: '期刊不存在'
}
class HTTP {
    request(params){
        return new Promise((resolve, reject) => {
            wx.request({
                url: config.apiBaseUrl + params.url,
                method: params.method || 'get',
                data: params.data || {},
                header: {
                    'content-type': 'application/json',
                    'appkey': config.appkey
                },
                success: (res) => {
                    const code = res.statusCode.toString();
                    if (code.startsWith('2')){    //成功
                        resolve(res.data);  
                    }else{
                        reject();
                        const error_code = res.data.error_code;
                        this.showError(error_code);
                    }
                },
                fail: (err) => {
                    reject(err);
                    this.showError(1);
                }
            });
        });
    };
    showError(error_code) {
        if (!error_code) {
            error_code = 1;
        }
        const tip = tips[error_code];
        wx.showToast({
            title: tip ? tip : tips[1],
            icon: 'none',
            mask: true,
            duration: 2000
        });
    }
}


export default HTTP;





